//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "InverseNonCentralChiSquareDistribution.h"
using namespace Cephei::QL::Math::Distributions;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Math::Distributions::CInverseNonCentralChiSquareDistribution::CInverseNonCentralChiSquareDistribution (Double df, Double ncp, Microsoft::FSharp::Core::FSharpOption<UInt64>^ maxEvaluations, Microsoft::FSharp::Core::FSharpOption<Double>^ accuracy) 
{
	_pSpinlock = new boost::detail::spinlock ();
    try
    {
#ifdef HANDLE
        _phInverseNonCentralChiSquareDistribution = NULL;
#endif
        QuantLib::Real _df = (QuantLib::Real)ValueHelper::Convert (df); //d
        QuantLib::Real _ncp = (QuantLib::Real)ValueHelper::Convert (ncp); //d
        QuantLib::Size _maxEvaluations = 
            (Microsoft::FSharp::Core::FSharpOption<UInt64>::IsSome::get (maxEvaluations) ? (QuantLib::Size)ValueHelper::Convert (maxEvaluations->Value) : 10); //4
        QuantLib::Real _accuracy = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (accuracy) ? (QuantLib::Real)ValueHelper::Convert (accuracy->Value) : 1e-8); //4
        _ppInverseNonCentralChiSquareDistribution = new boost::shared_ptr<QuantLib::InverseNonCentralChiSquareDistribution> (new QuantLib::InverseNonCentralChiSquareDistribution ( _df,  _ncp,  _maxEvaluations,  _accuracy ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Math::Distributions::CInverseNonCentralChiSquareDistribution::CInverseNonCentralChiSquareDistribution (boost::shared_ptr<QuantLib::InverseNonCentralChiSquareDistribution>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phInverseNonCentralChiSquareDistribution = NULL;
#endif
	_ppInverseNonCentralChiSquareDistribution = &childNative;
    
}
Cephei::QL::Math::Distributions::CInverseNonCentralChiSquareDistribution::CInverseNonCentralChiSquareDistribution (QuantLib::InverseNonCentralChiSquareDistribution& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phInverseNonCentralChiSquareDistribution = NULL;
#endif
	_ppInverseNonCentralChiSquareDistribution = new boost::shared_ptr<QuantLib::InverseNonCentralChiSquareDistribution> (&childNative);
    
    _InverseNonCentralChiSquareDistributionOwner = owner;
    
}

Cephei::QL::Math::Distributions::CInverseNonCentralChiSquareDistribution::CInverseNonCentralChiSquareDistribution (CInverseNonCentralChiSquareDistribution^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phInverseNonCentralChiSquareDistribution = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppInverseNonCentralChiSquareDistribution = new boost::shared_ptr<QuantLib::InverseNonCentralChiSquareDistribution> (copy->GetShared());
        
    }
}
Cephei::QL::Math::Distributions::CInverseNonCentralChiSquareDistribution::CInverseNonCentralChiSquareDistribution (PLATFORM::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phInverseNonCentralChiSquareDistribution = NULL;
#endif
	if (!t->IsSubclassOf(CInverseNonCentralChiSquareDistribution::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Math::Distributions::CInverseNonCentralChiSquareDistribution::CInverseNonCentralChiSquareDistribution (QuantLib::Handle<QuantLib::InverseNonCentralChiSquareDistribution>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phInverseNonCentralChiSquareDistribution = &childNative;
	_ppInverseNonCentralChiSquareDistribution = &static_cast<boost::shared_ptr<QuantLib::InverseNonCentralChiSquareDistribution>>(childNative.currentLink());
    
    _InverseNonCentralChiSquareDistributionOwner = owner;
}
Cephei::QL::Math::Distributions::CInverseNonCentralChiSquareDistribution::CInverseNonCentralChiSquareDistribution (QuantLib::Handle<QuantLib::InverseNonCentralChiSquareDistribution> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phInverseNonCentralChiSquareDistribution = &childNative;
	_ppInverseNonCentralChiSquareDistribution = &static_cast<boost::shared_ptr<QuantLib::InverseNonCentralChiSquareDistribution>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Math::Distributions::CInverseNonCentralChiSquareDistribution::CInverseNonCentralChiSquareDistribution (QuantLib::InverseNonCentralChiSquareDistribution childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phInverseNonCentralChiSquareDistribution = NULL;
#endif
	_ppInverseNonCentralChiSquareDistribution = new boost::shared_ptr<QuantLib::InverseNonCentralChiSquareDistribution> (new QuantLib::InverseNonCentralChiSquareDistribution (childNative));
    
}
#endif

Cephei::QL::Math::Distributions::CInverseNonCentralChiSquareDistribution::~CInverseNonCentralChiSquareDistribution ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppInverseNonCentralChiSquareDistribution != NULL)
    {
	    delete _ppInverseNonCentralChiSquareDistribution;
        _ppInverseNonCentralChiSquareDistribution = NULL;
    }
}
Cephei::QL::Math::Distributions::CInverseNonCentralChiSquareDistribution::!CInverseNonCentralChiSquareDistribution ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppInverseNonCentralChiSquareDistribution != NULL)
    {
	    delete _ppInverseNonCentralChiSquareDistribution;
    }
}
QuantLib::InverseNonCentralChiSquareDistribution& Cephei::QL::Math::Distributions::CInverseNonCentralChiSquareDistribution::GetReference ()
{
    if (_ppInverseNonCentralChiSquareDistribution == NULL) throw REFNEW NativeNullException ();
	return **_ppInverseNonCentralChiSquareDistribution;
}
boost::shared_ptr<QuantLib::InverseNonCentralChiSquareDistribution>& Cephei::QL::Math::Distributions::CInverseNonCentralChiSquareDistribution::GetShared ()
{
    if (_ppInverseNonCentralChiSquareDistribution == NULL) throw REFNEW NativeNullException ();
	return *_ppInverseNonCentralChiSquareDistribution;
}
QuantLib::InverseNonCentralChiSquareDistribution* Cephei::QL::Math::Distributions::CInverseNonCentralChiSquareDistribution::GetPointer ()
{
    if (_ppInverseNonCentralChiSquareDistribution == NULL) throw REFNEW NativeNullException ();
	return &**_ppInverseNonCentralChiSquareDistribution;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::InverseNonCentralChiSquareDistribution>& Cephei::QL::Math::Distributions::CInverseNonCentralChiSquareDistribution::GetHandle ()
{
	if (_phInverseNonCentralChiSquareDistribution == NULL)
	{
		_phInverseNonCentralChiSquareDistribution = new Handle<QuantLib::InverseNonCentralChiSquareDistribution> (*_ppInverseNonCentralChiSquareDistribution);
	}
	return *_phInverseNonCentralChiSquareDistribution;
}
#endif
bool Cephei::QL::Math::Distributions::CInverseNonCentralChiSquareDistribution::HasNative () 
{
	return (_ppInverseNonCentralChiSquareDistribution != NULL);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Math::Distributions::IInverseNonCentralChiSquareDistribution^ Cephei::QL::Math::Distributions::CInverseNonCentralChiSquareDistribution_Factory::Create (Double df, Double ncp, Microsoft::FSharp::Core::FSharpOption<UInt64>^ maxEvaluations, Microsoft::FSharp::Core::FSharpOption<Double>^ accuracy)
{
    return REFNEW CInverseNonCentralChiSquareDistribution ( df,  ncp,  maxEvaluations,  accuracy);
}
